﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Mengoo.DB;
using System.Collections;
using System.Data;

namespace Mengoo.BDMTier
{
    /// <summary>
    /// DAL的虚拟基类
    /// </summary>
    public abstract class DAO
    {
        #region 属性
        /// <summary>
        /// 操作的表名
        /// </summary>
        public string tableName = "";

        /// <summary>
        /// 操作的存储过程的前缀
        /// </summary>
        public string storePorcessPrix = "";

        /// <summary>
        /// 实体对应的记录关键字段名称
        /// </summary>
        public string keyField = "";

        /// <summary>
        /// 数据连接的类型
        /// </summary>
        public string connectionType = null;
        #endregion

        #region 基础方法 执行简单SQL

        #region Insert
        /// <summary>
        /// 添加一条记录
        /// </summary>
        /// <param name="model">记录实体</param>
        /// <returns></returns>
        public virtual int Add(object model)
        {
            return Add(model, "");
        }

        /// <summary>
        /// 添加一条记录
        /// </summary>
        /// <param name="model">记录实体</param>
        /// <param name="exceptColumns">不需要插入的列，用，分隔</param>
        /// <returns></returns>
        public virtual int Add(object model, string exceptColumns)
        {
            DAOHelper helper = CreateDAOHelper();
            return helper.Add(model, tableName, exceptColumns);
        }

        /// <summary>
        /// 添加多条记录
        /// </summary>
        /// <param name="modelList">记录集合</param>
        /// <returns></returns>
        public virtual int Add(List<object> modelList)
        {
            return Add(modelList, "");
        }

        /// <summary>
        /// 添加多条记录
        /// </summary>
        /// <param name="modelList">记录集合</param>
        /// <param name="exceptColumns">不需要插入的列，用，分隔</param>
        /// <returns></returns>
        public virtual int Add(List<object> modelList, string exceptColumns)
        {
            DAOHelper helper = CreateDAOHelper();
            return helper.Add(modelList, tableName, exceptColumns);
        }

        /// <summary>
        /// 添加一条记录
        /// </summary>
        /// <param name="columns">列的键值对</param>
        /// <returns></returns>
        public virtual int Add(Hashtable columns)
        {
            DAOHelper helper = CreateDAOHelper();
            return helper.Add(columns, tableName);
        }

        /// <summary>
        /// 添加多条记录
        /// </summary>
        /// <param name="columnsList">列的键值对</param>
        /// <returns></returns>
        public virtual int Add(List<Hashtable> columnsList)
        {
            DAOHelper helper = CreateDAOHelper();
            return helper.Add(columnsList, tableName);
        }
        #endregion

        #region Delete
        /// <summary>
        /// 删除记录，根据条件
        /// </summary>
        /// <param name="where">删除条件</param>
        /// <returns></returns>
        public virtual int Del(string where)
        {
            DAOHelper helper = CreateDAOHelper();
            return helper.Del(tableName, where);
        }

        /// <summary>
        /// 删除记录，根据记录实体
        /// </summary>
        /// <param name="model">记录实体</param>
        /// <returns></returns>
        public virtual int Del(object model)
        {
            return Del(model, keyField);
        }

        /// <summary>
        /// 删除记录，根据记录实体
        /// </summary>
        /// <param name="model">记录实体</param>
        /// <param name="keyField">关键字段名称</param>
        /// <returns></returns>
        public virtual int Del(object model, string keyField)
        {
            string where = SqlAdapt.AdaptKeyWhere(keyField, model);
            return Del(where);
        }

        /// <summary>
        /// 删除记录集合
        /// </summary>
        /// <param name="modelList">要删除的实体记录集合</param>
        /// <returns></returns>
        public virtual int Del(List<object> modelList)
        {
            return Del(modelList);
        }

        /// <summary>
        /// 删除记录集合
        /// </summary>
        /// <param name="modelList">要删除的实体记录集合</param>
        /// <param name="keyField">记录的关键字段名称</param>
        /// <returns></returns>
        public virtual int Del(List<object> modelList, string keyField)
        {
            List<string> whereList = new List<string>();
            foreach (object model in modelList)
            {
                string item = SqlAdapt.AdaptKeyWhere(keyField, modelList);
                whereList.Add(item);
            }

            string where = SqlAdapt.AdaptAnd(whereList);
            return Del(modelList);
        }
        #endregion

        #region Update
        /// <summary>
        /// 更新记录，根据实体和默认的关键字段名称
        /// </summary>
        /// <param name="model">数据实体</param>
        /// <returns></returns>
        public virtual int Update(object model)
        {
            return Update(model, keyField);
        }

        /// <summary>
        /// 更新记录，根据实体和关键字段名称
        /// </summary>
        /// <param name="model">数据实体</param>
        /// <param name="keyField">关键字字段</param>
        /// <returns></returns>
        public virtual int Update(object model, string keyField)
        {
            DAOHelper helper = new DAOHelper();
            string where = SqlAdapt.AdaptKeyWhere(keyField, model);
            return helper.Update(model, tableName, where);
        }

        /// <summary>
        /// 更新记录，根据列的键值对
        /// </summary>
        /// <param name="columns">列的键值对</param>
        /// <param name="where">更新条件</param>
        /// <returns></returns>
        public virtual int Update(Hashtable columns, string where)
        {
            DAOHelper helper = new DAOHelper();
            return helper.Update(columns, tableName, where);
        }
        #endregion

        #region Select
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <returns></returns>
        public virtual DataTable GetTableList()
        {
            return GetTableList("");
        }

        /// <summary>
        /// 获取列表，根据查询条件
        /// </summary>
        /// <param name="where">查询条件</param>
        /// <returns></returns>
        public virtual DataTable GetTableList(string where)
        {
            return GetTableList("", where);
        }
        /// <summary>
        /// 获取列表，根据查询条件，指定表名
        /// </summary>
        /// <param name="columns">要查询的列</param>
        /// <param name="where">查询条件</param>
        /// <returns></returns>
        public virtual DataTable GetTableList(string columns, string where)
        {
            DAOHelper helper = CreateDAOHelper();
            return helper.GetTableList(tableName, columns, where);
        }
        #endregion

        #endregion

        #region GetModel
        /// <summary>
        /// 获取Model
        /// </summary>
        /// <typeparam name="T">要获取的对象类型</typeparam>
        /// <param name="where">查询条件</param>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public T GetModel<T>(string keyValue)
        {
            string where = SqlAdapt.AdaptKeyWhere(keyField, keyValue);
            return GetModel<T>(where);
        }


        /// <summary>
        /// 获取Model
        /// </summary>
        /// <typeparam name="T">要获取的对象类型</typeparam>
        /// <param name="where">查询条件</param>
        /// <param name="columns">要查询的列</param>
        /// <returns></returns>
        public T GetModel<T>(string where,string columns)
        {
            DAOHelper helper = CreateDAOHelper();
            return helper.GetModel<T>(tableName, where, columns);
        }


        /// <summary>
        /// 获取ModelList
        /// </summary>
        /// <typeparam name="T">要获取的对象类型</typeparam>
        /// <param name="where">查询条件</param>
        /// <returns></returns>
        public List<T> GetModelList<T>(string where)
        {
            return GetModelList<T>(where, "");
        }

        /// <summary>
        /// 获取ModelList
        /// </summary>
        /// <typeparam name="T">要获取的对象类型</typeparam>
        /// <param name="where">查询条件</param>
        /// <param name="columns">要查询的列</param>
        /// <returns></returns>
        public List<T> GetModelList<T>(string where,string columns)
        {
            DAOHelper helper = CreateDAOHelper();
            return helper.GetModelList<T>(tableName, where, columns);
        }
        #endregion

        #region 基础方法，执行存储过程
        public virtual int AddByStoreProcess(object model)
        {
            return 0;
        }

        public virtual int DelByStoreProcess()
        {
            return 0;
        }

        public virtual int UpdateByStoreProcess()
        {
            return 0;
        }

        public virtual DataTable GetTableListByStoreProcess()
        {
            return new DataTable();
        }
        #endregion

        #region 私有方法
        /// <summary>
        /// 创建IDBHelper接口
        /// </summary>
        /// <returns></returns>
        private IDBHelper CreateDBHelper()
        {
            //读取webconfig中的配置作为数据库类型
            if (connectionType == null)
            {
                return DBFactory.CreateDBHelper();
            }
            return DBFactory.CreateDBHelper(connectionType);
        }

        /// <summary>
        /// 创建DAOHelper对象，用于数据库的交互
        /// </summary>
        /// <returns></returns>
        private DAOHelper CreateDAOHelper()
        {
            return new DAOHelper(connectionType);
        }
        #endregion

    }
}
